package com.cmf.myproject.integration.zentao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.alibaba.fastjson.TypeReference;
import com.cmf.myproject.integration.zentao.dto.ProjectInfoRsp;
import com.cmf.myproject.integration.zentao.dto.SessionRsp;
import com.cmf.myproject.integration.zentao.dto.UserTokenRsp;
import com.cmf.myproject.integration.zentao.dto.ZentaoBaseRsp;
import com.system.core.utils.HttpUtils;

import lombok.Data;

@Service
@Data
public class ZentaoClient {

	private String zentaoHost = "http://114.215.177.69:8877/zentao";
	private String userName = "jianglu";
	private String passwd = "jlcon123";

	public static String decodeUnicode(final String dataStr) {
		int start = 0;
		int end = 0;
		final StringBuffer buffer = new StringBuffer();
		while (start > -1) {
			end = dataStr.indexOf("\\u", start + 2);
			String charStr = "";
			if (end == -1) {
				charStr = dataStr.substring(start + 2, dataStr.length());
			} else {
				charStr = dataStr.substring(start + 2, end);
			}
			char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
			buffer.append(new Character(letter).toString());
			start = end;
		}
		return buffer.toString();
	}

	public ZentaoBaseRsp<SessionRsp> getsessionid() throws Exception {
		String result = HttpUtils.get(zentaoHost + "/api-getsessionid.json");
		result = result.replace("\\\"", "\"").replace("\"{", "{").replace("}\"", "}");
		ZentaoBaseRsp<SessionRsp> rsp = JSONObject.parseObject(result, new TypeReference<ZentaoBaseRsp<SessionRsp>>() {
		});
		return rsp;
	}

	public UserTokenRsp userLogin(String userName, String passwd, String sessionID) throws Exception {
		StringBuilder userLoginApi = new StringBuilder(zentaoHost + "/user-login.json");
		userLoginApi.append("?account=" + userName);
		userLoginApi.append("&password=" + passwd);
		userLoginApi.append("&zentaosid=" + sessionID);
		String result = HttpUtils.get(userLoginApi.toString());
		JSONObject resultJson = (JSONObject) JSONObject.parse(result);
		String status = resultJson.getString("status");

		UserTokenRsp userTokenRsp = new UserTokenRsp();
		userTokenRsp.setStatus(status);

		if ("failed".equalsIgnoreCase(status)) {
			userTokenRsp.setReason(resultJson.getString("reason"));
		} else {
			userTokenRsp.setToken(resultJson.getJSONObject("user").getString("token"));
		}
		return userTokenRsp;
	}

	public List<ProjectInfoRsp> projectAll(String zentaosid) throws Exception {
		List<ProjectInfoRsp> projects = new ArrayList<ProjectInfoRsp>();
		StringBuilder apistr = new StringBuilder(zentaoHost + "/project-all-all.json");
		apistr.append("?zentaosid=" + zentaosid);
		String result = HttpUtils.get(apistr.toString());
		result = result.replace("\\\"", "\"").replace("\\\\u", "\\u").replace("\"{", "{").replace("}\"", "}");
		System.out.println(result);
		JSONObject jobject = (JSONObject)JSONPath.read(result, "$.data.projects");
		jobject.entrySet().forEach((e)->{
			ProjectInfoRsp project = new ProjectInfoRsp();
			project.setId(e.getKey());
			project.setName((String)e.getValue());
		});
		return projects;
	}

	public static void main(String[] args) throws Exception {
		ZentaoClient zentaoClient = new ZentaoClient();
		String sessionId = zentaoClient.getsessionid().getData().getSessionID();
		zentaoClient.userLogin(zentaoClient.getUserName(), zentaoClient.getPasswd(), sessionId);
		zentaoClient.projectAll(sessionId);
	}
}
